package com.cortex.pineapple.listener;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class SlidListener implements OnTouchListener{

	private float startPositionX,endPositionX,slidFingerDistanceX;
	private float startPositionY,endPositionY,slidFingerDistanceY;
	private List<SlidDirection> directions;
	private int divideNumber;
	private OnSlidFingerListener onSlidFingerListener;
	int distanceToActionX,distanceToActionY;
	
	public SlidListener(int divideWidth,SlidDirection... dircationsArr) {
		init(divideWidth,dircationsArr);
	}
	public SlidListener(int divideWidth,boolean isMoveWork,SlidDirection... dircationsArr) {
		init(divideWidth,dircationsArr);
	}
	public SlidListener(int divideWidth,OnSlidFingerListener onSlidFingerListener,SlidDirection... dircationsArr) {
		this.onSlidFingerListener = onSlidFingerListener;
		init(divideWidth,dircationsArr);
	}
	
	
	private void init(int divideWidth,SlidDirection... dircationsArr){
		this.divideNumber = divideWidth;
		directions = new ArrayList<SlidListener.SlidDirection>();
		for (int i = 0; i < dircationsArr.length; i++) {
			directions.add(dircationsArr[i]);
		}
	}
	public void setListener(OnSlidFingerListener onSlidFingerListener){
		this.onSlidFingerListener = onSlidFingerListener;
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			 if (!v.hasFocus()) { 
                 v.requestFocus(); 
             } 
			startPositionX = event.getX();
			startPositionY = event.getY();
			onSlidFingerListener.startedSlid(v);
			
			return true;
			//break;
		case MotionEvent.ACTION_UP:
			endPositionX = event.getX();
			endPositionY = event.getY();
			calculateSlid(v);
			return slid(v);
		case MotionEvent.ACTION_POINTER_DOWN:
			break;
		case MotionEvent.ACTION_POINTER_UP:

		case MotionEvent.ACTION_MOVE: 
			break;
		}
		return false;
	}
	private void calculateSlid(View v){
		slidFingerDistanceX = endPositionX-startPositionX;
		slidFingerDistanceY = endPositionY-startPositionY;
		
		distanceToActionX = v.getWidth()/divideNumber;
		distanceToActionY = v.getHeight()/divideNumber;
	}
	private boolean slid(View v){
		if(distanceToActionX < Math.abs(slidFingerDistanceX)){
			if(directions.contains(SlidDirection.RIGHT)){
				if(slidFingerDistanceX>0){
					onSlidFingerListener.finishedSlid(v,SlidDirection.RIGHT);
					return true;
				}
			}
			if(directions.contains(SlidDirection.LEFT)){
				if(slidFingerDistanceX<0){
					onSlidFingerListener.finishedSlid(v,SlidDirection.LEFT);
					return true;
				}
			}
		}
		if(distanceToActionY < Math.abs(slidFingerDistanceY)){
			if(directions.contains(SlidDirection.UP)){
				if(slidFingerDistanceY<0){
					onSlidFingerListener.finishedSlid(v,SlidDirection.UP);
					return true;
				}
			}
			if(directions.contains(SlidDirection.DOWN)){
				if(slidFingerDistanceY>0){
					onSlidFingerListener.finishedSlid(v,SlidDirection.DOWN);
					return true;
				}
			}
		}
		if(directions.contains(SlidDirection.CLICK)){
			onSlidFingerListener.finishedSlid(v,SlidDirection.CLICK);
			return true;
		}
		return false;
	}
	public enum SlidDirection{
		UP,
		DOWN,
		RIGHT,
		LEFT,
		CLICK
	}
	public interface OnSlidFingerListener{
		void startedSlid(View v);
		void finishedSlid(View v,SlidDirection slidDirection);
	}
}